/*
 * Copyright (C) 2009 CityLights Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.schizoidman.android.citylights;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ServerPropertiesPage extends Activity {

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setResult(RESULT_CANCELED);
        
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.server_properties);

        ViewGroup container = (ViewGroup) findViewById(R.id.server_properties_view);
        AnimationSet set = new AnimationSet(true);

        Animation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(200);
        set.addAnimation(animation);

        animation = new TranslateAnimation(
            Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f
        );
        animation.setDuration(300);
        set.addAnimation(animation);

//        LayoutAnimationController controller =
//            new LayoutAnimationController(set, 0.25f);
//        container.setLayoutAnimation(controller);

//        ((TextView)findViewById(R.id.title)).setText("android-titlebar");

        // Init fields from source
        ((EditText)findViewById(R.id.url_edit)).setText(getIntent().getDataString());
        ((EditText)findViewById(R.id.username_edit)).setText(getIntent().getStringExtra("username"));
        ((EditText)findViewById(R.id.password_edit)).setText(getIntent().getStringExtra("password"));
        
        // Setting auto-complete adapted for URL field
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, new String[] {"http://"});
        AutoCompleteTextView url_edit = (AutoCompleteTextView)
                findViewById(R.id.url_edit);
        url_edit.setAdapter(adapter);
        url_edit.setValidator(new AutoCompleteTextView.Validator() {
			
			public boolean isValid(CharSequence text) {
				if (text != null && !text.toString().startsWith("http://")) {
					return false;
				}
				return true;
			}
			
			public CharSequence fixText(CharSequence invalidText) {
				return "http://" + invalidText.toString();
			}
		});


        final Intent result = new Intent(getIntent());
        
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title);
        ((Button)findViewById(R.id.title_right_btn)).setText("Done");
        ((Button)findViewById(R.id.title_right_btn)).setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Uri uri = Uri.parse(((EditText)findViewById(R.id.url_edit)).getText().toString());
				if (uri != null) {
					result.setData(uri);
					result.putExtra("username", ((EditText)findViewById(R.id.username_edit)).getText().toString());
					result.putExtra("password", ((EditText)findViewById(R.id.password_edit)).getText().toString());
					setResult(RESULT_OK, result);
					finish();
				}
				Toast.makeText(ServerPropertiesPage.this, R.string.malformed_url, Toast.LENGTH_LONG);
			}
		});

        container.startAnimation(animation);

    }
}
